//按键操作

#include "App_Main.h"
#include "Keyboard.h"
#include "apt32f102_syscon.h"


_key_param key_param;
uint8_t key_ONOFF;
uint8_t key_pset = 0; //P Set值,[0,5],默认0
uint8_t Prime_Enable = 0;
bool F_C = 0;
uint16_t Power_State = Temp_Shutoff;
//通过示波器测量按键延迟, 15ms: 计数: 2500 * 16
void keyboard_delay15ms(void)
{
    uint32_t i;

    // for (i = 0; i < 2500 * 16; i++)
    // {
    // }
    // delay_nms(15);
    delay_nms(12);
}

void Key_IOConfig(void)
{
    GPIO_Init(GPIOA0,IO_Key1,Intput);
    GPIO_Init(GPIOB0,IO_Key2,Intput);
    GPIO_Init(GPIOB0,IO_Key3,Intput);
    GPIO_Init(GPIOB0,IO_KeyLed,Output);
    KeyLed_OFF;

    GPIO_IntGroup_Set(GPIOA, IO_Key1, Selete_EXI_PIN0); //EXI13 set PA.13
    GPIOA0_EXI_Init(IO_Key1);
    EXTI_trigger_CMD(ENABLE,EXI_PIN13,_EXIFT); //_EXIFT下降沿触发, _EXIRT 上升沿触发
    EXTI_interrupt_CMD(ENABLE,EXI_PIN13);
    GPIO_EXTI_interrupt(GPIOA0,0x02000); //0x0023
    EXI4_Int_Enable(); //使能 GPIO 口外部中断
    // EXI1_WakeUp_Enable();  
}
void Key_init(void)
{
    Key_IOConfig();
    key_param.once1 = 0;
    key_param.once2 = 0;
    key_param.once3 = 0;
}

void Key_Power()
{
    if(key_param.once1 == 0)
    {
        if(Power_State == 0)
        {
			//HT162X_ResetRAM();
			UI_System.UI_Show = UI_ON;
            Power_State = 1;
        }
        else
		{  
			//HT162X_ResetRAM();
			Power_State = 0;
		}
	}
    key_param.once1 = 30;
}
//Key1,Key2,Key3
void KeyDetection(void)
{
    uint16_t temp;

    //key1
    // if(ReadIO_Key1 == 0)
    // {
    //     keyboard_delay15ms();
    //     if ((ReadIO_Key1 == 0) && (key_param.once1 == 0))
    //     {
    //         key_param.once1 = 1;
    //         // if(key_param.state_keyled == 0) ADInit();
    //     }
    // }
    // else if (ReadIO_Key1 > 0)
    // {
    //     keyboard_delay15ms();
    //     if(key_param.once1 > 0)
    //     {
    //         key_param.once1 = 0;

    //         if(UI_System.UI_Show == UI_OFF)
    //         {
    //             HT162X_Reset();
    //             UI_System.UI_Show = UI_ON;
    //         }
    //         else UI_System.UI_Show = UI_OFF;
    //     }
    // }

    if(UI_System.UI_Show != UI_ON) return;
    //key2
    if(ReadIO_Key2 == 0)
    { //Prime
        keyboard_delay15ms();
        if ((ReadIO_Key2 == 0) && (key_param.once2 == 0))
        {
            key_param.once2 = 1;

            if((HeatingFanStep >= 1) && (HeatingFanStep <= 3))
            {
                HeaterFanOpen;
                Prime_Enable = 1;
            }
        }
    }
    else if (ReadIO_Key2 > 0)
    {
        keyboard_delay15ms();
        if(key_param.once2 > 0) key_param.once2 = 0;
        if(Prime_Enable > 0)
        {
            Prime_Enable = 0;
            HeaterFanClose;
        }
    }
    //key3
    if(ReadIO_Key3 == 0)
    {
        keyboard_delay15ms();
        if ((ReadIO_Key3 == 0) && (key_param.once3 == 0))
        {
            key_param.once3 = 1;
            if(UI_System.Grill_Unit == Unit_C) UI_System.Grill_Unit = Unit_F;
            else UI_System.Grill_Unit = Unit_C; 
            Flash_Write();
        }
    }
    else if (ReadIO_Key3 > 0)
    {
        keyboard_delay15ms();
        if(key_param.once3 > 0)
        {
            key_param.once3 = 0;

            if((Temp_Param.oven_Err == Pt_Normal) && (Temp_Param.oven_set == Temp_Set_Smoke))
            {
                UI_System.Pset++;
                if(UI_System.Pset > PSet_7) UI_System.Pset = PSet_0;
                // Flash_Write();
            }
        }
    }

    if((temp_set == Temp_Set_500) && (key_param.once2 == 1) && (key_param.once3 == 1))
    {
        keyboard_delay15ms();
        if((key_param.once2 == 1) && (key_param.once3 == 1))
        {
            keyboard_delay15ms();
            if((key_param.once2 == 1) && (key_param.once3 == 1))
            {
                if(Prime_Enable > 0) Prime_Enable = 0;
            }
        }
    }
}

void EXI10to15IntHandler(void)
{
    if ((SYSCON->EXIRS & EXI_PIN13) == EXI_PIN13) 
    { //按键IO口外部中断触发
        SYSCON->EXICR = EXI_PIN13;

        Key_Power();

    }

    if ((SYSCON->EXIRS&EXI_PIN10)==EXI_PIN10) 
	{
		SYSCON->EXICR = EXI_PIN10;
	} 
	else if ((SYSCON->EXIRS&EXI_PIN11)==EXI_PIN11) 
	{
		SYSCON->EXICR = EXI_PIN11;
	} 
	else if ((SYSCON->EXIRS&EXI_PIN12)==EXI_PIN12) 
	{
		SYSCON->EXICR = EXI_PIN12;
	} 
	// else if ((SYSCON->EXIRS&EXI_PIN13)==EXI_PIN13) 
	// {
	// 	SYSCON->EXICR = EXI_PIN13;
	// }
	else if ((SYSCON->EXIRS&EXI_PIN14)==EXI_PIN14) 
	{
		SYSCON->EXICR = EXI_PIN14;
	}
	else if ((SYSCON->EXIRS&EXI_PIN15)==EXI_PIN15) 
	{
		SYSCON->EXICR = EXI_PIN15;
	}
}
